from rest_framework.generics import ListAPIView
from rest_framework.viewsets import ModelViewSet
from meiduo_mall.apps.meiduo_admin.my_pagination import MyPageNumberPagination
from . import sku_serializers

#1，ｓｋｕ管理


from meiduo_mall.apps.goods.models import *


class SKUModelViewSet(ModelViewSet):
    pagination_class = MyPageNumberPagination
    serializer_class = sku_serializers.SKUSerializers
    # queryset = SKU.objects.all()
    # print(queryset)
    #1,重写get_queryset，根据keywrod过滤ｓｋｕ
    def get_queryset(self):

        #1.获取keywrod
        keyword = self.request.query_params.get('keywrod')

        #2，根据keywrod查询sku
        if keyword:
            return SKU.objects.filter(name__contains=keyword).all()
        else:
            return SKU.objects.all()

class SKUCategoryView(ListAPIView):
    pagination_class = None
    serializer_class = sku_serializers.SKUCategorySerializers
    queryset = GoodsCategory.objects.filter(subs=None)


class GoodSimpleView(ListAPIView):
    pagination_class = None
    serializer_class = sku_serializers.GoodSimpleSerializers
    queryset = SPU.objects.all()